<?php
/***************************************************************
* SMFModCreator-Main.php                                       *
*--------------------------------------------------------------*
* SMF Mod Creator                                              *
* Version 0.1 Alpha                                            *
* Written By: Labradoodle-360                                  *
*--------------------------------------------------------------*
* Copyright 2011 Matthew Kerle - All Rights Reserved           *
*--------------------------------------------------------------*
* Redistribution and use in source and binary forms, with or   *
* without modification, are permitted provided that the        *
* conditions specified in the attached                         *
* smfmodcreator_license.txt file are met.                      *
***************************************************************/
if (!defined('SMF'))
	die ('Hacking Attempt...');

/* This function is the main handler for SMF Mod Creator */
function SMFModCreatorMain()
{

	// Globalize everything we'll need...
	global $context, $scripturl, $txt, $settings, $sourcedir, $page_links, $modifications;

	// Load SMFModCreator Template & Language
	loadTemplate('/smfmodcreator/SMFModCreator-Main');
	loadLanguage('/smfmodcreator/SMFModCreator');

	// Linktree!
	$context['linktree'][] = array(
		'url' => $scripturl . '?action=modcreator;page=view_mods',
		'name' => $txt['mod_creator'],
	);

	// Our Subs-Files.
	$subsFiles = array('Actions', 'Files', 'Main', 'XML');
	foreach ($subsFiles as $file)
		require_once($sourcedir . '/smfmodcreator/Subs-' . $file . '.php');

	// Action Handling
	$pages = array(
		'view_mods' => 'SMFModCreatorView',
		'create_mod' => 'SMFModCreatorCreate',
	);

	// Page Links
	$page_links = array(
		'create_mod' => array(
			'title' => $txt['smfmc_createmod'],
			'href' => $scripturl . '?action=modcreator;page=create_mod',
			'icon' => 'add_48.png',
		),
		'view_mods' => array(
			'title' => $txt['smfmc_viewmods'],
			'href' => $scripturl . '?action=modcreator;page=view_mods',
			'icon' => 'database_48.png',
		),
	);

	// A simple id->name array
	retrieve_modifications();

	// Template Layers
	$context['template_layers'][] = 'copyright';

	// HTML Headers = Our Head Content
	$context['html_headers'] .= '<link rel="stylesheet" type="text/css" href="' . $settings['default_theme_url'] . '/css/smfmodcreator/smfmodcreator.css" />';

	// Where shall we go?
	if (isset($_REQUEST['page']) && isset($pages[$_REQUEST['page']]) && !isset($_REQUEST['view_mod']) && !isset($_REQUEST['remove_mod']))
		$location = $pages[$_REQUEST['page']]();
	elseif (isset($_REQUEST['view_mod']) && !empty($_REQUEST['view_mod']) && !isset($_REQUEST['remove_mod']))
	{
		// Two ways to view a mod.
		$is_numeric = is_numeric($_REQUEST['view_mod']) ? 1 : 0;
		if (!empty($is_numeric) && isset($modifications[$_REQUEST['view_mod']]))
			loadModification($_REQUEST['view_mod'], $is_numeric);
		elseif (empty($is_numeric) && in_array($_REQUEST['view_mod'], $modifications))
			loadModification($_REQUEST['view_mod'], $is_numeric);
		else
			$pages['view_mods']();
	}
	else
		$pages['view_mods']();

	// Deleting is Separate...
	if (isset($_REQUEST['delete_mod']) && !empty($_REQUEST['delete_mod']))
	{
		// Removing a mod? We'll hand this over to Subs-Main.
		removeModification($_REQUEST['delete_mod']);
	}

	// !! For now, no guests, add actual permissions later.
	is_not_guest();

}

/* This function simply serves a list onto the template to show all of the modifications. */
function SMFModCreatorView()
{

	// Globalize everything we'll need...
	global $context, $sourcedir, $txt, $modSettings, $scripturl, $notifications;

	// jQuery
	$context['html_headers'] .= '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>';

	// Call our sub-template.
	$context['sub_template'] = 'creator_view';

	// Define our page title.
	$context['page_title'] = $txt['smfmc_allmods'];

	// Add a linktree item.
	$context['linktree'][] = array(
		'name' => $txt['smfmc_allmods'],
	);

	// Here you go, Nick ;)
	require_once($sourcedir . '/Subs-List.php');

	// Our list options.
	$listOptions = array(
		'id' => 'modifications',
		'items_per_page' => $modSettings['defaultMaxMessages'],
		'no_items_label' => $txt['smfmc_nomods'],
		'base_href' => $scripturl. '?action=modcreator;page=view_mods',
		'default_sort_col' => 'name',
		'get_items' => array(
			'function' => 'listModifications',
			'params' => array(),
		),
		'get_count' => array(
			'function' => 'modification_count',
			'params' => array(),
		),
		'columns' => array(
			'name' => array(
				'header' => array(
					'value' => 'Name',
				),
				'data' => array(
					'db' => 'name',
					'class' => 'centertext',
					'style' => 'width: 18%;',
				),
				'sort' => array(
					'default' => 'modi.mod_name',
					'reverse' => 'modi.mod_name DESC',
				),
			),
			'category' => array(
				'header' => array(
					'value' => 'Category',
				),
				'data' => array(
					'db' => 'category',
					'class' => 'centertext',
					'style' => 'width: 18%;',
				),
				'sort' => array(
					'default' => 'modi.id_category',
					'reverse' => 'modi.id_category DESC',
				),
			),
			'author' => array(
				'header' => array(
					'value' => $txt['author'],
				),
				'data' => array(
					'db' => 'author',
					'class' => 'centertext',
					'style' => 'width: 18%;',
				),
				'sort' => array(
					'default' => 'modi.author_id',
					'reverse' => 'modi.author_id DESC',
				),
			),
			'submitted' => array(
				'header' => array(
					'value' => $txt['smfmc_submitted'],
				),
				'data' => array(
					'db' => 'submitted',
					'class' => 'centertext',
					'style' => 'width: 18%;',
				),
				'sort' => array(
					'default' => 'modi.submitted',
					'reverse' => 'modi.submitted DESC',
				),
			),
			'modified' => array(
				'header' => array(
					'value' => $txt['smfmc_modified'],
				),
				'data' => array(
					'db' => 'modified',
					'class' => 'centertext',
					'style' => 'width: 18%;',
				),
				'sort' => array(
					'default' => 'modi.modified',
					'reverse' => 'modi.modified DESC',
				),
			),
			'actions' => array(
				'header' => array(
					'value' => $txt['smfmc_actions'],
				),
				'data' => array(
					'db' => 'actions',
					'class' => 'centertext',
					'style' => 'width: 6%;',
				),
			),
		),
	);

	// Our Notification System
	$notifications = array();
	if (isset($_REQUEST['notify']))
	{
		// jQuery Removal
		$context['html_headers'] .= '<script type="text/javascript">
		//<![CDATA[
			$(document).ready(function()
			{
				$(\'#profile_success\').delay(2000).slideUp(2500);
			});
		//]]>
		</script>';
		// Created
		if ($_REQUEST['notify'] === 'created')
			$notifications['created'] = 'Modification Successfully Created!';
		// Removed
		elseif ($_REQUEST['notify'] === 'removed')
			$notifications['removed'] = 'Modification Successfully Removed!';
	}

	// Create the list
	createList($listOptions);

}

/* This function loads everything required for the Mod/Single Project View */
function loadModification($modID = '', $is_name = '')
{

	// Globalize everything we'll need here...
	global $context, $scripturl, $modifications, $txt, $modification;

	// Be here you shouldn't.
	if (empty($modID))
		return;

	// Passing a name, are we?
	if (empty($is_name))
	{
		$flipped_modifications = array_flip($modifications);
		$modID = $flipped_modifications[$modID];
	}

	// Modification Arrays
	singleModification($modID);

	// Linktree[]
	$context['linktree'][] = array(
		'url' => $scripturl . '?action=modcreator;view_mod=' . $modID,
		'name' => $modification['name'],
	);

	// Page title
	$context['page_title'] = $modification['name'];

	// Sub-Template!
	$context['sub_template'] = 'project_view';

}

/* The "Create Modification" page handler function */
function SMFModCreatorCreate()
{

	// Globalize everything we'll need...
	global $context, $sourcedir, $txt, $modSettings, $scripturl;
	global $categories, $smcFunc, $errors, $modifications;

	// Set up our page title.
	$context['page_title'] = $txt['smfmc_createmod'];

	// Add a linktree item.
	$context['linktree'][] = array(
		'url' => $scripturl . '?action=modcreator;page=create_mod',
		'name' => $txt['smfmc_createmod'],
	);

	// Attempting to Create?
	if (isset($_POST['submitted']))
	{

		// Initial Checks.
		$errors = array();
		if (empty($_POST['mod_name']))
			$errors['mod_name'] = 'You must enter a name for your modification.';
		elseif (!empty($_POST['mod_name']))
		{
			$original_length = strlen($_POST['mod_name']);
			$_POST['mod_name'] = preg_replace('/^[^a-zA-Z]{1,}|(^|$(\s)){1,}/', '', $_POST['mod_name']);
			if (empty($_POST['mod_name']))
				$errors['mod_name'] = 'The modificiation name must have alphabetical characters.';
			elseif ($original_length > strlen($_POST['mod_name']))
				$errors['mod_name'] = 'The modification name you entered was invalid.<ul><li>Please note numbers & symbols are not allowed to prefix a mod name.</li></ul>';
		}
		if (empty($_POST['id_category']))
			$errors['id_category'] = 'You must select a category for your modification.';

		$_POST['identifier'] = $smcFunc['strtolower'](str_replace(' ', '_', $_POST['mod_name']));
		if (in_array($_POST['identifier'], $modifications))
			$errors['duplicate_name'] = 'A modification already exists with this name.';

		// No errors? Submit
		if (empty($errors))
		{
			createMod($_POST);
		}
	}

	// Sub-Template.
	$context['sub_template'] = 'creator_create';

	// Our Categories
	retrieveCategories();

}

?>